﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ComLib.ComFun;
using ComLib.ComType;
using ComLib.ConfigService;

namespace ProcessConfig.ConfigType
{
    [XmlRootAttribute("PhotoQC", Namespace = "ProcessConfig.ConfigType", IsNullable = false)]
    public class QCOptions : XmlSerializeBase
    {
        public QCOptions()
        {
            _crj = new QCCheckOption();
            _visa = new QCCheckOption();
        }

        /// <summary>
        /// 照片一所质检选项(身份证)
        /// </summary>
        private QCCheckOption _visa = new QCCheckOption(false);
        public QCCheckOption Visa
        {
            get { return _visa; }
            set { _visa = value; }
        }

        /// <summary>
        /// ftp照片一所质检选项（出入境）
        /// </summary>
        private QCCheckOption _crj = new QCCheckOption(true);
        public QCCheckOption CRJ
        {
            get { return _crj; }
            set { _crj = value; }
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            QCOptions obj = (QCOptions)Serializer.LoadFromXml(ConfigFilePath, this.GetType());
            if (obj != null)
            {
                this._nVer = obj._nVer;
                this._crj = obj._crj;
                this._visa = obj._visa;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("照片一所质检选项: ");
            FillFieldValueStringDelegate FillString =
                (string field, object val) =>
                {
                    builder.Append("[");
                    builder.Append(field);
                    builder.Append("]: ");
                    builder.Append(val.ToString() + "; ");
                };
            FillString("出入境", _crj);
            FillString("口岸签证", _visa);  
            return builder.ToString();
        }

        public object Clone()
        {
            QCCheckOption opt0 = (QCCheckOption)(_crj.Clone() as QCCheckOption);
            QCCheckOption opt1 = (QCCheckOption)(_visa.Clone() as QCCheckOption);
            QCOptions opts = new QCOptions();
            opts._crj = opt0;
            opts._visa = opt1;
            return opts;
        }
    }

    public class QCCheckOption
    {
        public QCCheckOption()
        {

        }

        public QCCheckOption(bool enabled)
        {
            _enabled = enabled;
        }
        /// <summary>
        /// 使用模式: 0 可选; 1 强制
        /// </summary>
        private QCMode _useMode = QCMode.Mandatory;
        public QCMode UseMode
        {
            get { return _useMode; }
            set { _useMode = value; }
        }

        /// <summary>
        /// 是否可用
        /// </summary>
        private bool _enabled = true;
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        /// <summary>
        /// 加密类型
        /// </summary>
        private EncryptMode _encryptType = EncryptMode.Hardware;
        public EncryptMode EncryptType
        {
            get { return _encryptType; }
            set { _encryptType = value; }
        }

        public int HandleType()
        {
            if(_encryptType == EncryptMode.Hardware)
            {
                if (_useMode == QCMode.Mandatory)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            else
            {
                if (_useMode == QCMode.Mandatory)
                {
                    return 2;
                }
                else
                {
                    return 3;
                }
            }
        }

        public object Clone()
        {
            QCCheckOption clone = new QCCheckOption();
            clone._enabled = _enabled;
            clone._encryptType = _encryptType;
            clone._useMode = _useMode;
            return clone;
        }

        public override string ToString()
        {
            if (!_enabled)
            {
                return "[一所质检]功能未启用; ";
            }
            StringBuilder builder = new StringBuilder("[一所质检] 已启用, 使用模式为:");
            builder.Append(_useMode == QCMode.Optional ? "可选模式" : "强制模式");
            builder.Append("加密类型: " + _encryptType.ToString());
            return builder.ToString();
        }
    }
}
